package com.lyw.login.vm

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.lu.library.base.BaseViewModel
import com.lu.library.base.IUiIntent
import com.lu.library.base.IUiState
import com.lu.library.ext.launchSafe
import com.lu.library.ext.postEvent
import com.lu.library.http.ApiCoroutineExceptionHandler
import com.lu.library.kv.KV
import com.lu.library.kv.KVDelegate
import com.lu.library.utils.CountDown
import com.lyw.login.Constants
import com.lyw.login.api.LoginApi
import com.lyw.login.bean.LoginEvent
import com.lyw.login_api.LoginConstants
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.lang.Thread.sleep
import javax.inject.Inject

sealed class RegisterUiState:IUiState{
    data object INIT : RegisterUiState()
    data object LoginSuccess : RegisterUiState()
    data class CountDown(val countDown:String,val codeEnable:Boolean):RegisterUiState()
    //是否登录可以点击
    data class LoginEnable(var loginEnable: Boolean):RegisterUiState();
    //是否发送验证码可以点击
    data class SendCodeEnable(var codeEnable: Boolean):RegisterUiState();
}

sealed class RegisterIntent:IUiIntent{
    data class Register(val name: String, val pwd: String) : RegisterIntent()
    data class SendCode(val phone:String): RegisterIntent()
    data class PhoneChange(var phone:String):RegisterIntent()
    data class PwdChange(var pwd:String):RegisterIntent()
    data class CodeChange(var code:String):RegisterIntent()
}
@HiltViewModel
class RegisterVm  @Inject constructor(
    val kv:KV,
    val api:LoginApi
):BaseViewModel<RegisterUiState,RegisterIntent>() {
//    private val kv by lazy {
//        MMKVImpl()
//    }
    private var userName by KVDelegate(key = Constants.KEY_USER_NAME,def = "")
    private var pwd by KVDelegate(key = Constants.KEY_USER_PWD,def = "")


    private var countDown = 6;
    override fun initUiState(): RegisterUiState {
        return RegisterUiState.INIT;
    }
    private var isCoding = false
    private var code:String = ""
    private var tempPwd:String = ""
    private var tempPhone:String = ""

    override fun handleIntent(intent: RegisterIntent) {
        when(intent){
            is RegisterIntent.Register->{
                login(intent)
            }
            is RegisterIntent.SendCode ->{
                startCountDown()
            }
            is RegisterIntent.PhoneChange->{
                tempPhone = intent.phone
                updateUiState {
                    RegisterUiState.LoginEnable(tempPhone.isNotEmpty()&&pwd.isNotEmpty()&&code.isNotEmpty())
                }
                if (!isCoding){
                    updateUiState {
                        RegisterUiState.SendCodeEnable(tempPhone.isNotEmpty())
                    }
                }
            }
            is RegisterIntent.PwdChange->{
                tempPwd = intent.pwd
                updateUiState {
                    RegisterUiState.LoginEnable(tempPhone.isNotEmpty()&&pwd.isNotEmpty()&&code.isNotEmpty())
                }
            }
            is RegisterIntent.CodeChange->{
                code = intent.code
                updateUiState {
                    RegisterUiState.LoginEnable(tempPhone.isNotEmpty()&&pwd.isNotEmpty()&&code.isNotEmpty())
                }
            }
            else ->{

            }
        }
    }
    private fun startCountDown() {
        viewModelScope.launch {
            isCoding = true
            CountDown.start(6, { t ->
                updateUiState {
                    RegisterUiState.CountDown(t.toString() + "s", false)
                }
            }) {
                isCoding = false
                updateUiState {
                    RegisterUiState.CountDown("发送验证码", true)
                }
            }
        }
    }
    private fun login(intent:RegisterIntent.Register){
        viewModelScope.launchSafe(Dispatchers.Main) {
//            val userInfo = withContext(Dispatchers.IO) {
                userName = intent.name
            pwd = intent.pwd
//                api.login(LoginReq(intent.name, intent.pwd)).data
//            }
//            userInfo?.let {
//                isLogin = true
//                kv.put(LoginConstants.KEY_USER_INFO,GsonUtils.toJson(it))
//            }
//            LogUtils.d(userInfo)
            updateUiState {
                RegisterUiState.LoginSuccess
            }
            LoginEvent().postEvent()
        }
    }
}